﻿using Dapper;
using System.Data.SqlClient;
using yunbo.Model.SM;

namespace yunbo.Repositorys.FPMRepositorys
{
    public class WMS_CCPRKRepository
    {
        public string data = "Data Source=121.37.93.71;Initial Catalog=FanXingMES;User ID=sa;Password=geili@2025;";

        public int AddWMS_CCPRK(WMS_CCPRK wMS_CCPRK)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"insert into wms_cprk(cprk_code,cprk_lx,cprk_gdh,cprk_bz,create_by,create_time,cprk_zt)\r\n" +
                    $"values('{wMS_CCPRK.cprk_code}','{wMS_CCPRK.cprk_lx}','{wMS_CCPRK.cprk_gdh}','{wMS_CCPRK.cprk_bz}',{wMS_CCPRK.create_by},'{wMS_CCPRK.create_time}',{wMS_CCPRK.cprk_zt})";
                int rs = connection.Execute(sql );
                return rs;
            }
        }
        public int UpZt(string code)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"update [dbo].[wms_cprk] set [cprk_zt]=2 where [cprk_code]='{code}'";
                int rs = connection.Execute(sql);
                return rs;
            }
        }
        public FYWMS_CCPRK FYWMS_CCPRK(int PageNumber, int PageSize) 
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = @$"select
                                cprk.*,
                                us.[user_name] as user1_name
                                from [dbo].[wms_cprk] cprk
                                left join
                                [dbo].[user] us 
                                on cprk.create_by= us.user_id
                                ORDER BY [cprk_id]
                                OFFSET ({PageNumber}-1)*{PageSize} ROWS
                                FETCH NEXT {PageSize} ROWS ONLY;";
                List<WMS_CCPRK> cp = connection.Query<WMS_CCPRK>(sql).ToList();
                string sql1 = $"SELECT COUNT(*) FROM [dbo].[wms_cprk]";
                int zon = connection.Query<int>(sql1).FirstOrDefault();
                FYWMS_CCPRK fYWMS_CCPRK = new FYWMS_CCPRK()
                {
                    wMS_CCPRKs = cp,
                    zon = zon,
                };
                return fYWMS_CCPRK;

            }
        }
    }
}
